package com.example.medicalequipment.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.example.medicalequipment.R;
import com.example.medicalequipment.entity.MedicalDevice;

import java.util.List;

public class RepairAdapter extends RecyclerView.Adapter<RepairAdapter.RepairViewHolder> {
    private List<MedicalDevice> repairList;
    private Context context;
    private OnItemClickListener listener;

    public RepairAdapter(List<MedicalDevice> repairList, Context context) {
        this.repairList = repairList;
        this.context = context;
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    @NonNull
    @Override
    public RepairViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_repair_request, parent, false);
        return new RepairViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RepairViewHolder holder, int position) {
        MedicalDevice device = repairList.get(position);
        holder.tvDeviceName.setText(device.getName());
        holder.tvDepartment.setText("科室: " + device.getDepartment());
        holder.tvReporter.setText("报修人: " + device.getReporter());

        // 设置状态文本和颜色
        String statusText = "";
        int statusColor = 0;

        switch (device.getStatus()) {
            case 0:
                statusText = "待处理";
                statusColor = ContextCompat.getColor(context, R.color.status_pending);
                break;
            case 1:
                statusText = "处理中";
                statusColor = ContextCompat.getColor(context, R.color.status_processing);
                break;
            case 2:
                statusText = "已完成";
                statusColor = ContextCompat.getColor(context, R.color.status_completed);
                break;
        }

        holder.tvStatus.setText(statusText);
        holder.tvStatus.setTextColor(statusColor);

        // 设置点击事件
        holder.itemView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onItemClick(position, device);
            }
        });
    }

    @Override
    public int getItemCount() {
        return repairList.size();
    }

    public interface OnItemClickListener {
        void onItemClick(int position, MedicalDevice device);
    }

    public static class RepairViewHolder extends RecyclerView.ViewHolder {
        TextView tvDeviceName, tvDepartment, tvReporter, tvStatus;

        public RepairViewHolder(@NonNull View itemView) {
            super(itemView);
            tvDeviceName = itemView.findViewById(R.id.tv_item_device_name);
            tvDepartment = itemView.findViewById(R.id.tv_item_department);
            tvReporter = itemView.findViewById(R.id.tv_item_reporter);
            tvStatus = itemView.findViewById(R.id.tv_item_status);
        }
    }
}
